package com.zzb.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RabbitConfig {
    private static final String TOPIC_EXCHANGE = "zzb.in_home.topic";

    @Bean
    public Queue userQueue() {
        log.info("userQueue 实例化成功...");
        // 如果rabbit 里没有这个队列，会创建该队列
        return new Queue("zzb_user",true);
    }

    @Bean
    public Queue productQueue() {
        log.info("productQueue 实例化成功...");
        // 如果rabbit 里没有这个队列，会创建该队列
        return new Queue("zzb_product",true);
    }

    @Bean
    TopicExchange zzbInHomeTopicExchange() {
        // 如果rabbit 里没有这个exchange，则会创建一个
        log.info("Topic 实例化成功...,name={}",TOPIC_EXCHANGE);
        return new TopicExchange(TOPIC_EXCHANGE,true,false);
    }

    /*
        绑定队列
     */
    @Bean
    Binding userQueue2WxInHomeTopic() {
        // 绑定队列到topic,这里的routingKey是binding key.
        log.info(" userQueue 绑定 Topic 成功");
        return BindingBuilder.bind(userQueue()).to(zzbInHomeTopicExchange()).with("zzb.user.*");
    }
    @Bean
    Binding productQueue2WxInHomeTopic() {
        // 绑定队列到topic,这里的routingKey是binding key.
        log.info(" productQueue 绑定 Topic 成功");
        return BindingBuilder.bind(productQueue()).to(zzbInHomeTopicExchange()).with("zzb.product.*");
    }

}
